<template>
  <div class="container">
    <el-card>
      <div slot="header">
        <span style="font-weight: 700; font-size: 22px">基本资料</span>
      </div>
      <el-form
        ref="userForm"
        :model="userForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="登录名称" prop="username">
          <el-input disabled v-model="userForm.username" style="width: 40%"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="userForm.nickname" style="width: 40%"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="userForm.email" style="width: 40%"></el-input>
        </el-form-item>
        <el-form-item label-width="60px">
          <el-button type="primary" size="small" @click="btnOK"
            >提交修改</el-button
          >
          <el-button type="info" plain @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getUserInfo, updateUserInfo } from '@/api/user'
export default {
  name: 'baseInfo',
  data () {
    return {
      userForm: {
        username: '',
        nickname: '',
        email: ''
      },
      rules: {
        nickname: [
          { required: true, message: '昵称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '昵称在2-10位之间', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getUserInfo()
  },
  methods: {
    async btnOK () {
      await this.$refs.userForm.validate()
      await updateUserInfo(this.userForm)
      this.$message.success('基本资料修改成功')
      location.reload()
    },
    reset () {
      this.userForm = {
        username: '',
        nickname: '',
        email: ''
      }
      this.$refs.userForm.resetFields()
    },
    async getUserInfo () {
      const res = await getUserInfo()
      this.userForm = res.data
    }
  }
}
</script>

<style>
</style>
